package MLMaliciousURLFinder;
import java.net.*;
import java.io.*;
import Utility.*;

public class google {
	
	private URL url;							//url :-)
	private boolean googleResult;
	
	public boolean getGoogleResult() {
		return googleResult;
	}

	google(String url_string) throws MalformedURLException, UnknownHostException
	{
		try
		{
			url = new URL(url_string);
			googleResult = retreiveGoogleResult();
		}
		catch (MalformedURLException e)
		{
			log("Invalid URL: " + url_string);
		}
	}
	
	private void log(String message)
	{
		try
		{
			BufferedWriter f = new BufferedWriter(new FileWriter("google_log.txt", true));
			f.write(message);
			f.close();
		}
		catch(IOException e)
		{
			//System.out.println("Trouble writing to file for URL " + URL + ": " + e);
		}
	}
	
	/*  
	 *  visits google's diagnostic site with url as parameter,
	 *  looks for string that indicates the page is not malicious,
	 *  if found, returns false, otherwise true
	 */
	private boolean retreiveGoogleResult()
	{
		String s; //line of response
		String googleUrlString = "http://www.google.com/safebrowsing/diagnostic?site=" + url.toString();
		try
		{
			URL googleUrl = new URL(googleUrlString);
			HttpURLConnection connection = (HttpURLConnection)googleUrl.openConnection();
			connection.connect();
			InputStream inputStream = (InputStream)connection.getContent();
			BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
			while((s = reader.readLine())!= null)
			{
				if(s.indexOf("This site is not currently listed as suspicious.") >=0)
				{
					return false;
				}
			}			
		}
		catch(IOException ioe)
		{
			log("Error in getting google diagnostics info " + ioe.getMessage()+ "\n");
		}
		return true;		
	}
	
	public static void main(String args[])
	{
		String filename = "urls.txt";
		String line;
		try {
			BufferedReader input =  new BufferedReader(new FileReader(filename));
			
			while((line = input.readLine()) != null)
			{
				
			}
		}
		catch (IOException e) {
			Utility.logMessage("Error, could not open file: " + filename, "featureExtractor_errorlog.txt");
		}
		
//		Connection conn = null;
		
//		try {
//			Class.forName("org.sqlite.JDBC");
//		    conn =
//		      DriverManager.getConnection("jdbc:sqlite:urls.db");
//			
//			Statement stmt = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY,
//                    							 ResultSet.CONCUR_READ_ONLY);
//			ResultSet srs = stmt.executeQuery("SELECT tweet_id, user_id, text FROM URLMap");
//			
//			while(srs.next())
//			{
//				String text = srs.getString("text");
//				featureExtractor fe = new featureExtractor(text);
//				if (fe.getGoogleResult())
//				{
//					long tweet_id = srs.getLong("tweet_id");
//					int user_id = srs.getInt("user_id");
//					PreparedStatement addMalURL = conn.prepareStatement(
//				    "INSERT INTO maliciousURLMap VALUES(?,?,?)");
//					
//					addMalURL.setLong(1, tweet_id);
//					addMalURL.setInt(2, user_id);
//					addMalURL.setString(3, text);
//					
//					addMalURL.execute();
//				}
//			}
//		}
//		catch(Exception e) {
//			e.printStackTrace();
//		}
	}
}
